<?php

/**
 * @Author: wuchenge
 * @Date:   2022-04-08 18:04:01
 * @Email: ligui0506@126.com
 * @Descript:
 */
namespace App\Admin\Controllers;

use App\Http\Controllers\ApiController;
use App\Models\HotLabel;
use App\Requests\HotRequest;
use Illuminate\Http\Request;

class HotController extends ApiController
{

    public function add(HotRequest $request)
    {
        $name = $request->input('name', '');

        $sort = HotLabel::getMaxSort();

        $res = HotLabel::create([
            'name' => $name,
            'sort' => $sort,
        ]);

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    public function modify(HotRequest $request)
    {
        $name = $request->input('name', '');
        $id   = $request->input('id', 0);

        $res = HotLabel::where('id', $id)->update(['name' => $name]);

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    public function detail($id)
    {
        $res = HotLabel::where('id', $id)->first();
        return $this->response($res);
    }

    public function del(Request $request)
    {
        $id  = $request->input('id', 0);
        $res = HotLabel::where('id', $id)->delete();
        if ($res) {
            return $this->response();
        }
        return $this->failed();
    }

    public function lists(Request $request)
    {
        $name  = $request->input('name', '');
        $model = HotLabel::select(['*']);
        if (!empty($name)) {
            $model->where('name', 'like', '%' . $name . '%');
        }
        $data = $model->orderBy('sort', 'asc')->get()->toArray();
        return $this->response($data);
    }
}
